package net.itbaiyang.pattern.factory;

import jakarta.mail.internet.MimeMessage;
import net.itbaiyang.pattern.email.bridge.mailType.*;
import net.itbaiyang.pattern.enums.MailTypeEnum;
import net.itbaiyang.pattern.model.EmailDetails;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class DefaultEmailFactory implements MailMessageFactory{
    @Override
    public MimeMessageHelper createEmail(JavaMailSender mailSender, String from, MailStencil template) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        template.apply(helper);
        return helper;
    }

    @Override
    public MailStencil createMailStencil(EmailDetails details) {

        MailTypeEnum mailType = MailTypeEnum.fromString(details.getType());
        MailStencil mailStencil;
        switch (mailType) {
            case SIMPLE_MAIL:
                return mailStencil = new SimpleMail(details);
            case HTML_MAIL:
                return mailStencil = new HtmlMail(details);
            case ATTACH_MAIL:
                return mailStencil = new AttachmentsMail(details);
            case INLINE_MAIL:
                return mailStencil = new InlineResourceMail(details);
            default:
                return null;
        }
    }
}
